package com.shigu.searchWord.action;

import com.shigu.MallResponse;
import com.shigu.PageResponse;
import com.shigu.TokenRequest;
import com.shigu.exceptions.WapException;
import com.shigu.searchWord.api.SearchWordService;
import com.shigu.searchWord.bo.WordIdRequest;
import com.shigu.searchWord.vo.HistorySearchVo;
import com.shigu.searchWord.vo.HotWordVo;
import com.shigu.user.service.MallTokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.validation.Valid;

/*
 * create by  过儿  2019-06-05
 *
 */
@Controller
@RequestMapping("wapsjson")
public class SearchWordController {

    @Resource(name="searchWordServiceImpl")
    SearchWordService searchWordService;
    @Autowired
    MallTokenService mallTokenService;

    @RequestMapping("getHistorySearchWordList")
    @ResponseBody
    public PageResponse<HistorySearchVo> getHistorySearchWordList(TokenRequest request){
        Long mallUserId = mallTokenService.selUserIdByToken(request.getToken());
        return searchWordService.getHistorySearchWordList(mallUserId);
    }

    @RequestMapping("deleteHistorySearchWord")
    @ResponseBody
    public MallResponse deleteHistorySearchWord(@Valid WordIdRequest request, BindingResult result) throws WapException {
        if(result.hasErrors()){
            throw new WapException(result.getFieldError().getDefaultMessage());
        }
        searchWordService.deleteWord(Long.parseLong(request.getWordId()));
        return new MallResponse().success();
    }

    @RequestMapping("getHotSearchWordList")
    @ResponseBody
    public PageResponse<HotWordVo> getHotSearchWordList(){
        return searchWordService.getHotSearchWordList();
    }

}
